import { defineStore } from "pinia";
import { LoginKeys } from "./keys";

interface IUserState {
  menus: IMenu[];
  username: string;
}

type IMyData = IMenu & { children: IMenu[] };
export const useStore = defineStore(LoginKeys, {
  state(): IUserState {
    return {
      menus: [],
      username: "",
    };
  },
  getters: {
    rotes() {
      let menus: IMyData[] = [];
      menus = this.menus
        .filter((item) => item.parentId === 0)
        .map((items) => ({
          ...items,
          children: [],
        }));
      this.menus.forEach((item) => {
        const num = menus.find((item2) => item2.id === item.parentId);
        if (num) {
          num.children?.push(item);
        }
      });
      return menus;
    },
    permissions(): string[] {
      return this.menus.map((item) => item.name);
    },
  },
});
